package com.yiban.demo.Access.Entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.yiban.demo.Global.Entity.EntityGroup.NeedIdGroup;
import com.yiban.demo.Global.Entity.EntityGroup.NoNeedIdAccessGroup;
import com.yiban.demo.LoginUser.Entity.ROLE;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@NoArgsConstructor
@Data
@AllArgsConstructor
public class Access {
    @NotNull(message = "id不能为空" ,groups = NeedIdGroup.class)
    @TableId(type = IdType.AUTO)
    private Integer accessId;
    @NotNull(message = "role不能为空",groups = NoNeedIdAccessGroup.class)
    private String role;

    @NotNull(message = "uri信息为空",groups = NoNeedIdAccessGroup.class)
    @Valid
    private RestfulUri restfulUri;

    /**
     *
     * @param needCheckNull 是否只检测非null的属性，true -> 存在null则直接不通过 false -> 跳过开始检测下一项
     * @return
     */
    public List<String> checkField(boolean needCheckNull){
       ArrayList<String> info = new ArrayList<>();
       if (needCheckNull){
           if (role == null) info.add("role为空");

           else if (!ROLE.hasRole(role)) info.add("不存在该role");

           if (restfulUri == null) info.add("restfulUri为空");
           else{
               if (restfulUri.getUri() == null) info.add("uri为空");

               if (restfulUri.getMethod() == null) info.add("method为空");
               else {
                   try {
                       RequestMethod.valueOf(restfulUri.getMethod());
                   } catch (IllegalArgumentException e) {
                       info.add("不支持的method");
                   }
               }
           }
       }else {
           if (role != null && !ROLE.hasRole(role) ) info.add("不存在该role");

           if (restfulUri != null) {
               if (restfulUri.getMethod() != null) {
                   try {
                       RequestMethod.valueOf(restfulUri.getMethod());
                   } catch (IllegalArgumentException e) {
                       info.add("不支持的method");
                   }
               }
           }
       }
       return info;
    }
}
